<template>
  <div
    class="comment-view"
    ref="commentView"
    v-if="item"
    @scroll="commentScroll"
  >
    <div class="top-nav">
      <div class="left-icon" @click="goBack">
        <van-icon name="arrow-left" />
      </div>
      <div class="title-wrap">
        <div class="bg"></div>
        <div class="title">糗事百科</div>
      </div>
      <div class="right-icon">
        <van-icon name="ellipsis" />
      </div>
    </div>
    <div class="top-content" ref="topContent" v-if="!item.distance">
      <VideoCom
        :scrollTop="scrollTop"
        :contentData="item"
        :isAutoplay="true"
        :loading="loading"
        :isFollow="item.isFollow"
        :isDislike="item.isDislike"
        :isLike="item.isLike"
        @getFollowState="getFollowState(item)"
        @getLikeState="getLikeState(item)"
        @getDislikeState="getDislikeState(item)"
        @addShareCount="addShareCount(item)"
        @deleteData="deleteData(item)"
      />
    </div>
    <div class="top-content" ref="topContent" style="margin-bottom:20px;" v-if="item.distance">
      <dynamicCom
        :contentData="item"
        @toLike="toLike(item)"
        @getFollowState="getFollowState(item)"
        @addShareCount="addShareCount(item)"
        @deleteData="deleteData(item)"
        @goTocomment="goTocomment(item)"
      />
    </div>
    <div class="bottom-toolbar">
      <div class="to-comment">
        <input
          class="comment-ipt"
          type="text"
          placeholder="评论一下，踢你上去!"
          v-model="toComment"
          @keydown.enter="toCommentHandel"
        />
      </div>
      <div class="toolbar">
        <div class="icon" v-for="(item, index) in iconData" :key="index">
          <van-icon :name="item.icon" />
        </div>
      </div>
    </div>
    <div class="hot-comment" ref="hotComment" v-if="hotComment.length > 0">
      <div class="title">热门评论 {{ hotComment.length }}</div>
      <div class="content-wrap">
        <div
          class="content-list"
          v-for="(item, index) in hotComment"
          :key="item.id"
        >
          <div class="user-top">
            <div class="user-message">
              <div class="imgbox">
                <img :src="item.user.thumb" alt="" />
              </div>
              <div class="textbox">
                <div class="textbox-top">{{ item.user.login }}</div>
                <div class="textbox-bottom">27天前 {{ item.ip_location }}</div>
              </div>
            </div>
            <div class="right-icon">
              <div class="icon-select"><van-icon name="ellipsis" /></div>
              <div
                class="icon-like"
                :class="{ active: item.isLike == true }"
                @click="toLike(item)"
              >
                <van-icon name="good-job-o" />
              </div>
              <div class="like-count">{{ item.like_count }}</div>
            </div>
          </div>
          <div class="content-text">{{ item.content }}</div>
          <div class="go-to-comment-comment" @click="goToCommentInside(item)">
            {{ item.children.length }}条回复<van-icon name="arrow" />
          </div>
          <div class="line" v-if="index != hotComment.length"></div>
        </div>
      </div>
    </div>
    <div class="new-comment" ref="newComment" v-if="newShowComment">
      <div class="title">最新评论 {{ newShowComment.length }}</div>
      <div class="content-wrap">
        <div
          class="content-list"
          v-for="(item, index) in newShowComment"
          :key="item.id"
        >
          <div class="user-top">
            <div class="user-message">
              <div class="imgbox">
                <img :src="item.user.thumb" alt="" />
              </div>
              <div class="textbox">
                <div class="textbox-top">{{ item.user.login }}</div>
                <div class="textbox-bottom">27天前 {{ item.ip_location }}</div>
              </div>
            </div>
            <div class="right-icon">
              <div class="icon-select"><van-icon name="ellipsis" /></div>
              <div
                class="icon-like"
                :class="{ active: item.isLike == true }"
                @click="toLike(item)"
              >
                <van-icon name="good-job-o" />
              </div>
              <div class="like-count">{{ item.like_count }}</div>
            </div>
          </div>
          <div class="content-text">{{ item.content }}</div>
          <div class="go-to-comment-comment">回复</div>
          <div class="line" v-if="index < newComment.length"></div>
        </div>
      </div>
    </div>
    <div class="go-back-to-top" @click="goToTop" v-if="goToTopIconState">
      <van-icon name="arrow-up" /><br />返回<br />顶部
    </div>
    <van-action-sheet v-model:show="vanActionShow" title="回复">
      <div class="comment-comment-content">
        <div
          class="comment-comment-content-list"
          v-for="(item, index) in commentInside"
          :key="item.id"
        >
          <div class="user-top">
            <div class="user-message">
              <div class="imgbox">
                <img :src="item.user.thumb" alt="" />
              </div>
              <div class="textbox">
                <div class="textbox-top">{{ item.user.login }}</div>
                <div class="textbox-bottom">27天前 {{ item.ip_location }}</div>
              </div>
            </div>
            <div class="right-icon">
              <div class="icon-select"><van-icon name="ellipsis" /></div>
              <div
                class="icon-like"
                :class="{ active: item.isLike == true }"
                @click="toLike(item)"
              >
                <van-icon name="good-job-o" />
              </div>
              <div class="like-count">{{ item.like_count }}</div>
            </div>
          </div>
          <div class="content-text">{{ item.content }}</div>
          <div class="go-to-comment-comment">回复</div>
          <div class="line" v-if="index < newComment.length"></div>
        </div>
        <div class="bottom-toolbar">
          <div class="to-comment">
            <input
              class="comment-ipt"
              type="text"
              placeholder="评论一下，踢你上去!"
              v-model="toComment2"
              @keydown.enter="toCommentHandel2"
            />
          </div>
          <div class="toolbar">
            <div class="icon" v-for="(item, index) in iconData" :key="index">
              <van-icon :name="item.icon" />
            </div>
          </div>
        </div>
        <h1 class="comment-inside-title" v-if="commentInside.length == 0">
          别让楼主寂寞太久哦~
        </h1>
      </div>
    </van-action-sheet>
  </div>
</template>

<script>
import { getCommentContent } from "@/api/comment";
import VideoCom from "@/components/HomeCom/VideoCom.vue";
import dynamicCom from '@/components/DynamicCom/dynamicCom.vue'
import { throttle } from "@/utils/throttle";

export default {
  name: "CommentView",
  components: {
    VideoCom,
    dynamicCom,
  },
  data() {
    return {
      //底部图标数据
      iconData: [
        {
          icon: "fire-o",
        },
        {
          icon: "gift-o",
        },
        {
          icon: "smile-o",
        },
        {
          icon: "photo-o",
        },
      ],
      //内容数据
      item: null,
      //全部评论数据
      contentData: [],
      //加载状态
      loading: true,
      //获取数据的id
      dataId: 0,
      //定时器
      timer: null,
      //滚动距离
      scrollTop: 0,
      //热门评论数据
      hotComment: [],
      //最新评论数据
      newComment: [],
      //截取的最新评论数据
      newShowComment: [],
      //返回顶部图标显示状态
      goToTopIconState: false,
      //容器总高度
      totalHeight: 0,
      //截取评论数
      index1: 0,
      index2: 9,
      //新增评论
      toComment: "",
      toComment2: "",
      //评论的评论显示状态
      vanActionShow: false,
      //评论里的评论
      commentInside: [],
    };
  },
  activated() {
    this.item = JSON.parse(window.localStorage.getItem("commentContentData"));
    if (this.dataId != this.$route.query.id && this.$route.query.id) {
      this.dataId = this.$route.query.id;
      this.getCommentContentData();
    }
    this.$forceUpdate();
  },
  watch: {
    scrollTop(newval) {
      if (newval + 500 > this.totalHeight && newval < this.totalHeight) {
        if (this.loading) {
          this.loading = false;
          this.index1 += 10;
          this.index2 += 10;
          this.getNewShowComment();
        }
      }
      if (newval >= this.totalHeight && !this.loading) {
        this.$toast({
          message: "没有更多啦~",
          position: "bottom",
        });
      }
    },
    newShowComment() {
      this.timer = setTimeout(() => {
        this.getTotalHeight();
        this.timer = null;
      }, 100);
    },
  },
  methods: {
    //获取评论数据
    getCommentContentData() {
      this.newShowComment = [];
      this.index1 = 0;
      this.index2 = 9;
      getCommentContent({ axios: this.$axios }, this.$route.query.id).then(
        ({ data }) => {
          // console.log(data);
          data.hot_comments.forEach((item) => {
            item["isLike"] = false;
          });
          this.hotComment = data.hot_comments;
          data.items.forEach((item) => {
            item["isLike"] = false;
          });
          this.newComment = data.items;
          for (let i = this.index1; i <= this.index2; i++) {
            this.newShowComment.push(this.newComment[i]);
          }
          // this.contentData.push(...data.items);
          // console.log(this.contentData);
        }
      );
    },
    //获取截取的最新评论实现懒加载
    getNewShowComment() {
      for (let i = this.index1; i <= this.index2; i++) {
        if (i == this.newComment.length) {
          break;
        }
        this.newShowComment.push(this.newComment[i]);
      }
    },
    //关注
    getFollowState(item) {
      item.isFollow = !item.isFollow;
      let followedData = JSON.parse(
        window.localStorage.getItem("followedData")
      );
      let idArr = [];
      followedData.forEach((item) => {
        idArr.push(item.id);
      });
      if (item.isFollow && idArr.indexOf(item.user.id) == -1) {
        followedData.push(item.user);
        window.localStorage.setItem(
          "followedData",
          JSON.stringify(followedData)
        );
      } else {
        // console.log(followedData.indexOf(item.user));
        followedData.forEach((item2, index) => {
          if (item2.id == item.user.id) {
            followedData.splice(index, 1);
            window.localStorage.setItem(
              "followedData",
              JSON.stringify(followedData)
            );
          }
        });
      }
    },
    //点赞
    getLikeState(item) {
      item.isLike = !item.isLike;
      if (item.isLike) {
        item.isDislike = false;
        item.votes.up++;
      } else {
        item.votes.up--;
      }
    },
    //踩
    getDislikeState(item) {
      item.isDislike = !item.isDislike;
      if (item.isDislike && item.isLike) {
        item.isLike = false;
        item.votes.up--;
      }
    },
    //分享
    addShareCount(item) {
      item.share_count++;
    },
    //删除
    deleteData(item) {
      this.$dialog
        .confirm({
          title: "确定要删除吗?",
        })
        .then(() => {
          // on confirm
          this.contentData.forEach((item2, index) => {
            if (item2 == item) {
              this.contentData.splice(index, 1);
            }
          });
        })
        .catch(() => {
          // on cancel
        });
    },
    //获取已关注
    getFollowedData() {
      let followedData = JSON.parse(
        window.localStorage.getItem("followedData")
      );
      let followedDataId = [];
      followedData.forEach((item) => {
        followedDataId.push(item.id);
      });
      this.contentData.forEach((item) => {
        if (followedDataId.indexOf(item.user.id) != -1) {
          item.isFollow = true;
        } else {
          item.isFollow = false;
        }
      });
    },
    //路由返回一级
    goBack() {
      this.$router.go(-1);
    },
    //评论点赞
    toLike(item) {
      item.isLike = !item.isLike;
      if (item.isLike) {
        item.like_count++;
      } else {
        item.like_count--;
      }
    },
    //滚动方法
    commentScroll: throttle(function () {
      this.scrollTop = this.$refs.commentView.scrollTop;
      if (this.scrollTop >= 150) {
        this.goToTopIconState = true;
      } else {
        this.goToTopIconState = false;
      }
    }, 500),
    //返回顶部方法
    goToTop() {
      this.$refs.commentView.scrollTo(0, 0);
    },
    //获取容器总高度
    getTotalHeight() {
      this.totalHeight =
        this.$refs.topContent.clientHeight +
        this.$refs.hotComment.clientHeight +
        this.$refs.newComment.clientHeight -
        this.$refs.commentView.clientHeight;
    },
    //外层评论
    toCommentHandel() {
      if (this.toComment.length > 0 && this.toComment != " ") {
        let inputComment = {
          content: this.toComment,
          id: new Date().getTime(),
          like_count: 0,
          isLike: false,
          created: new Date().getTime(),
          user: {
            id: new Date().getTime(),
            login: "玛卡巴卡",
            thumb:
              "http://pic.qiushibaike.com/system/avtnew/4518/45181736/thumb/default_avatar.webp",
          },
        };
        this.newShowComment.unshift(inputComment);
        this.toComment = "";
      }
    },
    //内层评论
    toCommentHandel2() {
      if (this.toComment2.length > 0 && this.toComment2 != " ") {
        let inputComment = {
          content: this.toComment2,
          id: new Date().getTime(),
          like_count: 0,
          isLike: false,
          created: new Date().getTime(),
          user: {
            id: new Date().getTime(),
            login: "玛卡巴卡",
            thumb:
              "http://pic.qiushibaike.com/system/avtnew/4518/45181736/thumb/default_avatar.webp",
          },
        };
        this.commentInside.unshift(inputComment);
        this.toComment2 = "";
      }
    },
    //前往内层评论
    goToCommentInside(item) {
      this.vanActionShow = !this.vanActionShow;
      item.children.forEach((item) => {
        item["isLike"] = false;
      });
      this.commentInside = item.children;
      // console.log(this.commentInside);
    },
  },
};
</script>

<style lang="scss" scoped>
.comment-view {
  width: 100vw;
  height: 100vh;
  padding: 50px 0;
  overflow: auto;
  .top-nav {
    width: 100vw;
    height: 50px;
    border-bottom: 1px solid #ddd;
    box-sizing: border-box;
    background-color: #fff;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px;
    position: fixed;
    top: 0;
    left: 0;
    .left-icon {
      font-size: 20px;
    }
    .title-wrap {
      display: flex;
      align-items: center;
      .bg {
        width: 25px;
        height: 25px;
        border-radius: 5px;
        overflow: hidden;
        margin-right: 5px;
        background: url("@/assets/images/ic_launcher.png") no-repeat center
          center;
        background-size: cover;
      }
      .title {
        font-size: 15px;
        font-weight: 600;
      }
    }
    .right-icon {
      font-size: 25px;
    }
  }
  .top-content {
    padding: 10px;
  }
  .bottom-toolbar {
    width: 100vw;
    height: 50px;
    box-sizing: border-box;
    border-top: 1px solid #ddd;
    z-index: 6666;
    background-color: #fff;
    position: fixed;
    bottom: 0;
    left: 0;
    display: flex;
    padding: 7.5px 20px;
    .to-comment {
      width: 200px;
      height: 35px;
      position: relative;
      input[type="text"]::placeholder {
        color: #000;
      }
      .comment-ipt {
        padding-left: 10px;
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: rgb(247, 247, 247);
        border-radius: 5px;
      }
    }
    .toolbar {
      display: flex;
      padding: 5px 10px;
      .icon {
        width: 25px;
        height: 25px;
        margin-right: 10px;
        font-size: 25px;
      }
    }
  }
  .hot-comment {
    width: 100%;
    margin-top: -20px;
    padding: 10px;
    box-sizing: border-box;
    // padding-bottom: ;
    .title {
      font-weight: 600;
      margin-bottom: 20px;
    }
    .content-wrap {
      width: 100%;
      border-bottom: 1px solid #ddd;
      padding-bottom: 20px;
      .content-list {
        width: 100%;
        .user-top {
          width: 100%;
          display: flex;
          align-items: center;
          justify-content: space-between;
          .user-message {
            display: flex;
            align-items: center;
            .imgbox {
              width: 35px;
              height: 35px;
              border-radius: 999px;
              overflow: hidden;
              margin-right: 10px;
              background-color: rgba(0, 0, 0, 0.35);
            }
            .textbox {
              .textbox-top {
                font-size: 14px;
                color: #888;
                margin-bottom: 5px;
              }
              .textbox-bottom {
                color: #bbb;
              }
            }
          }
          .right-icon {
            display: flex;
            align-items: center;
            color: #bbb;
            margin-right: 20px;
            .icon-select {
              font-size: 20px;
              margin-right: 20px;
            }
            .icon-like {
              font-size: 20px;
              margin-right: 5px;
              &.active {
                color: rgb(186, 163, 37);
              }
            }
          }
        }
        .content-text {
          width: 100%;
          padding: 15px 20px 30px 42px;
          font-size: 15px;
          box-sizing: border-box;
        }
        .go-to-comment-comment {
          width: calc(100% - 62px);
          padding-left: 10px;
          margin-left: 40px;
          background-color: rgb(249, 249, 249);
          height: 35px;
          border-radius: 8px;
          box-sizing: border-box;
          color: rgb(51, 188, 229);
          line-height: 35px;
        }
        .line {
          width: calc(100% - 62px);
          height: 1px;
          background-color: #ddd;
          margin: 20px 0 10px 40px;
          .none-line {
            display: none;
          }
        }
      }
    }
  }
  .new-comment {
    width: 100%;
    margin-top: 20px;
    padding: 10px;
    box-sizing: border-box;
    .title {
      font-weight: 600;
      margin-bottom: 20px;
    }
    .content-wrap {
      width: 100%;
      border-bottom: 1px solid #ddd;
      .content-list {
        width: 100%;
        .user-top {
          width: 100%;
          display: flex;
          align-items: center;
          justify-content: space-between;
          .user-message {
            display: flex;
            align-items: center;
            .imgbox {
              width: 35px;
              height: 35px;
              border-radius: 999px;
              overflow: hidden;
              margin-right: 10px;
              background-color: rgba(0, 0, 0, 0.35);
            }
            .textbox {
              .textbox-top {
                font-size: 14px;
                color: #888;
                margin-bottom: 5px;
              }
              .textbox-bottom {
                color: #bbb;
              }
            }
          }
          .right-icon {
            display: flex;
            align-items: center;
            color: #bbb;
            margin-right: 20px;
            .icon-select {
              font-size: 20px;
              margin-right: 20px;
            }
            .icon-like {
              font-size: 20px;
              margin-right: 5px;
              &.active {
                color: rgb(186, 163, 37);
              }
            }
          }
        }
        .content-text {
          width: 100%;
          padding: 15px 20px 30px 42px;
          font-size: 15px;
          box-sizing: border-box;
        }
        .go-to-comment-comment {
          margin-left: 40px;
          color: rgb(51, 188, 229);
        }
        .line {
          width: calc(100% - 62px);
          height: 1px;
          background-color: #ddd;
          margin: 20px 0 10px 40px;
        }
      }
    }
  }
  .go-back-to-top {
    width: 70px;
    height: 70px;
    font-size: 20px;
    background-color: rgb(255, 223, 51);
    text-align: center;
    line-height: 20px;
    border-radius: 999px;
    position: fixed;
    right: -7px;
    bottom: 90px;
    transform: scale(0.5);
    z-index: 99;
  }
  ::v-deep .van-popup{
    z-index: 7000 !important;
  }
  .comment-comment-content {
    padding: 10px;
    font-size: 16px;
    height: 170px;
    overflow: auto;
    padding-bottom: 50px;
    .comment-comment-content-list {
      margin-bottom: 10px;
      .user-top {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .user-message {
          display: flex;
          align-items: center;
          .imgbox {
            width: 35px;
            height: 35px;
            border-radius: 999px;
            overflow: hidden;
            margin-right: 10px;
            background-color: rgba(0, 0, 0, 0.35);
          }
          .textbox {
            .textbox-top {
              font-size: 14px;
              color: #888;
              margin-bottom: 5px;
            }
            .textbox-bottom {
              color: #bbb;
            }
          }
        }
        .right-icon {
          display: flex;
          align-items: center;
          color: #bbb;
          margin-right: 20px;
          .icon-select {
            font-size: 20px;
            margin-right: 20px;
          }
          .icon-like {
            font-size: 20px;
            margin-right: 5px;
            &.active {
              color: rgb(186, 163, 37);
            }
          }
        }
      }
      .content-text {
        width: 100%;
        padding: 15px 20px 30px 42px;
        font-size: 15px;
        box-sizing: border-box;
      }
      .go-to-comment-comment {
        margin-left: 40px;
        color: rgb(51, 188, 229);
      }
      .line {
        width: calc(100% - 62px);
        height: 1px;
        background-color: #ddd;
        margin: 20px 0 10px 40px;
      }
      &:last-of-type {
        margin-bottom: 0;
      }
    }
    .bottom-toolbar {
      width: 100vw;
      height: 50px;
      box-sizing: border-box;
      border-top: 1px solid #ddd;
      z-index: 6668;
      background-color: #fff;
      position: fixed;
      bottom: 0;
      left: 0;
      display: flex;
      padding: 7.5px 20px;
      .to-comment {
        width: 200px;
        height: 35px;
        position: relative;
        input[type="text"]::placeholder {
          color: #000;
        }
        .comment-ipt {
          padding-left: 10px;
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background-color: rgb(247, 247, 247);
          border-radius: 5px;
        }
      }
      .toolbar {
        display: flex;
        padding: 5px 10px;
        .icon {
          width: 25px;
          height: 25px;
          margin-right: 10px;
          font-size: 25px;
        }
      }
    }
  }
  .comment-inside-title {
    width: 100%;
    text-align: center;
    color: #bbb;
  }
}
</style>